package com.sjq.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.sjq.common.entity.LoginAccount;
import com.sjq.common.web.SessionUserHelper;



public class SessionTimeOutInterceptor extends HandlerInterceptorAdapter {
	
	private static final String login_url="/view/login/login.jsp";
	
	private Logger logger=Logger.getLogger(SessionTimeOutInterceptor.class);
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler)
	throws Exception{
		try {
			  LoginAccount obj=SessionUserHelper.create(request).getLoginAccount();
		} catch (RuntimeException e) {
			if(request.getHeader("x-requested-with")!=null
					&& "XMLHttpRequest".equalsIgnoreCase(request.getHeader("x-request-with")) ){
				response.setHeader("sessionstatus", "timeout");//在响应头设置session状态
			}else{
				//直接提交到最高层次页面
				response.getWriter().write("<script>top.document.location.href='"+request.getContextPath()+login_url+"'</script>");
				response.getWriter().flush();
			}
			
			return false;
		}
      return super.preHandle(request, response, handler);
	}


	
	
    
}
